#include <ros/ros.h>
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>
#include <yaml_test/yaml_function.h>
YAML::const_iterator find_name_return_it(std::string fin, std::string name_);
std::string fin = "/home/bingo/study_ws/src/yaml_test/yaml_test.yaml";       //yaml文件所在的路径..
YAML::Node yamlConfig = YAML::LoadFile(fin);
//可以自定义模板的，但是感觉有点复杂，暂时先用着
int main(int argc, char **argv)
{
    int i = 0;
    std::string name = "bedroom";
    YAML::const_iterator yaml_it = find_name_return_it(fin,name);
    std::cout << yaml_it->first.as<std::string>() << ":" << yaml_it->second.as<std::string>() << std::endl;
    std::cout << "yamlConfig[name].size:" <<  yamlConfig[name].size() << std::endl;
    ++yaml_it;
    //不知道为什么用迭代器 还会读取多一笔 null
    //while(yaml_it != yamlConfig[name_].end())
    for (i = 0; i<(yamlConfig[name].size()-1); i++)
    {
        std::cout << yaml_it->first.as<std::string>() << ":" << yaml_it->second.as<float>() << std::endl;
        ++yaml_it;
    } 
    std::cout <<"finish"<< std::endl;
    std::ofstream file;
    file.open(fin);
    file.flush();
    file << yamlConfig;
    file.close();
 
    return 0;
}
YAML::const_iterator find_name_return_it(std::string fin, std::string name_)
{
    int yaml_size = 0;
    YAML::Node yamlConfig = YAML::LoadFile(fin);
    YAML::const_iterator it = yamlConfig.begin();
    yaml_size = yamlConfig.size(); //获取yalm的size
    for(int i = 1;i<=yaml_size;i++)
    {
        if(it->first.as<std::string>() == name_)
        {
            std::cout << "found:"  << name_<< std::endl;
            return yamlConfig[name_].begin();
        }
        it++;

    }
    std::cout << "can not found:"  << name_<< std::endl;
    exit(-1);
}